package com.mobipe.sutto.netty;

import java.net.InetSocketAddress;
import java.util.concurrent.Executors;

import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;

import com.mobipe.sutto.core.AbstractServer;
import com.mobipe.sutto.core.ThreadFactoryImpl;

/**
 * @author kohachiro
 *
 */
public class Server extends AbstractServer{
    private ServerBootstrap bootstrap;
    private static Server instance;

    /**
	 * 
	 */
	public Server() {
        bootstrap = new ServerBootstrap(
                new NioServerSocketChannelFactory(
                        Executors.newCachedThreadPool(new ThreadFactoryImpl("NETTYSERVER-BOSS-")),
                        Executors.newCachedThreadPool(new ThreadFactoryImpl("NETTYSERVER-WORKER-"))));
		bootstrap.setOption("tcpNoDelay", true);
		//bootstrap.setOption("reuseAddress", true);
        bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
            public ChannelPipeline getPipeline() throws Exception {
                return Channels.pipeline(new ServerHandler());
            }
        });
	}

	@Override
	public void bind() throws Exception {
        System.out.println("Listening on port " + PORT);		
		instance.bootstrap.bind(new InetSocketAddress(PORT));
	}
	
	public static void main(String[] args) throws Exception {
		instance=new Server();
		instance.start();
		instance.bind();
    }

	@Override
	public void start() {
		// TODO Auto-generated method stub
		
	}	
}
